<?php 
include_once('comm/smarty.php');
include_once('comm/conn.php');
include_once('comm/user.class.php');
$users = new User();
$usersList = $users->fetch_all(); 
$allusers = $usersList['data']; // return all datas.
$rowsCount = $usersList['length']; // count data rows.
$pageRows = 10 ; // how many datas fetch in every page.
$pageCount = ceil($usersList['length'] / $pageRows); // pages length count.

if(!empty($_GET['p'])){
    if($_GET['p'] <= $pageCount && $_GET['p'] > 1 ){ // current page index number.
      $currPage = $_GET['p'];
    }elseif ($_GET['p'] > $pageCount) {
      $currPage = $pageCount; }
    else{ 
      $currPage = 1;
    }
    $startRow = ( $currPage - 1 ) * 10; // set the SQL query linmit starter.
  }else{
    $currPage = 1;
    $startRow = 0;
  }

$paging = $users->paging($startRow,$pageRows);  // get paging datas;

$indexLength = 5; // set the length of paging handle.

$pageIndexs = array(); // paging handle numbers.
if ($currPage >= $indexLength ) { // previou numbers.
  for ($i=2; $i >= 0 ; $i--) { 
    $pageIndexs[] = floor($currPage / $indexLength) * $indexLength - $i;
  }
}
for ($i=1; $i <= $indexLength; $i++) { // next numbers.
   $result= floor($currPage / $indexLength) * $indexLength + $i;
   if ($result <= $pageCount) {
     $pageIndexs[] = $result;
   };
}

if ($currPage > 1) { // previou page index
  $prevPage = $currPage - 1 ;
}else{
  $prevPage = false;
};
if ($currPage < $pageCount) { // next page index
  $nextPage = $currPage + 1 ;
}else{
  $nextPage = false;
};





$assData = array('users'=>$paging,
'currPage'=>$currPage,
'pageIndexsArr'=>$pageIndexs,
'prev'=>$prevPage,
'next'=>$nextPage);
$smarty->assign('data',$assData);
$smarty->display("users.tpl");
 ?>